package cn.com.fuhuitong.main.mine.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import cn.com.fuhuitong.app.BaseApplication
import cn.com.fuhuitong.base.BaseViewModel
import cn.com.fuhuitong.http.BaseResponse
import cn.com.fuhuitong.http.HttpResponse
import cn.com.fuhuitong.http.ViewModelSubscribeListener
import cn.com.fuhuitong.main.home.entity.CharityListResponse
import cn.com.fuhuitong.main.home.entity.HomeResponse
import cn.com.fuhuitong.main.mine.entity.*
import cn.com.fuhuitong.main.mine.mode.MineManager
import cn.com.fuhuitong.main.order.mode.OrderManager
import cn.com.fuhuitong.utils.*
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

class AddressViewModel(application: Application) : BaseViewModel(application) {


    private val mineManager: MineManager = MineManager()

    val addressDefaultDataLiveData = MutableLiveData<HttpResponse<AddressDefaultResponse>>()

    val addressListDataLiveData = MutableLiveData<HttpResponse<AddressListResponse>>()

    val baseDataLiveData = MutableLiveData<HttpResponse<BaseResponse>>()

    // 选中的省市区信息
    val selectProvince: SelectProvinceEntity = SelectProvinceEntity()

    // 所有的省市区编码列表
    var provinceList: List<ProvinceEntity>? = null
        private set

    // 给OptionPickerView提供的数据
    var provinceUIData: ProvinceUIData? = null

    /**
     * 获取默认地址
     * */
    fun addressDefault() {
        val listener =
            object :
                ViewModelSubscribeListener<AddressDefaultResponse>(addressDefaultDataLiveData) {}
        val memberItemDisposable: Disposable = mineManager.addressDefault(
            BaseApplication.userToken(), listener
        )
        dispose.add(memberItemDisposable)
    }

    /**
     * 获取地址列表
     */
    fun addressList() {
        val listener =
            object : ViewModelSubscribeListener<AddressListResponse>(addressListDataLiveData) {}
        val addressListDisposable: Disposable = mineManager.addressList(
            BaseApplication.userToken(), listener
        )
        dispose.add(addressListDisposable)
    }

    /**
     * 添加地址
     */
    fun addressAdd(address: AddressAddEntity) {
        val listener =
            object : ViewModelSubscribeListener<BaseResponse>(baseDataLiveData) {}
        val addressAddDisposable: Disposable = mineManager.addressAdd(
            BaseApplication.userToken(), address, listener
        )
        dispose.add(addressAddDisposable)
    }

    /**
     * 添加地址
     */
    fun addressEdit(address: AddressAddEntity) {
        val listener =
            object : ViewModelSubscribeListener<BaseResponse>(baseDataLiveData) {}
        val addressEditDisposable: Disposable = mineManager.addressEdit(
            BaseApplication.userToken(), address, listener
        )
        dispose.add(addressEditDisposable)
    }



    /**
     * 获取省市区列表,并根据addressId判断是否需要加载数据
     */
    fun getProvinceFromAssets() {
        val province = Observable.create(ObservableOnSubscribe<List<ProvinceEntity>> {
            val provinceList = ProvinceListUtils.getProvinceList(getApplication())
            it.onNext(provinceList)
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe({
            provinceList = it
            provinceUIData = ProvinceListUtils.getProvinceUIList(provinceList!!)
        }, { it.printStackTrace() })
        dispose.add(province)
    }

    /**
     * 根据三个code，获取对应的省市区名称，拼接起来
     */
    fun queryAddressString(provinceCode: Int, cityCode: Int, areaCOde: Int): String = buildString {
        provinceList?.find { it.provinceId == provinceCode }?.also {
            selectProvince.provinceId = it.provinceId
            selectProvince.provinceName = it.provinceName
        }?.cityList?.find { it.cityId == cityCode }?.also {
            selectProvince.cityId = it.cityId
            selectProvince.cityName = it.cityName
        }?.areaList?.find { it.id == areaCOde }?.run {
            selectProvince.areaId = id
            selectProvince.areaName = areaName
            append(selectProvince.provinceName).append(selectProvince.cityName).append(selectProvince.areaName)
        }
    }

}